package com.zh.jwt.service.impl;

import com.auth0.jwt.internal.org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.jwt.common.enums.BizEnum;
import com.zh.jwt.common.exception.BizException;
import com.zh.jwt.common.jwt.JwtUtil;
import com.zh.jwt.common.model.ApiResult;
import com.zh.jwt.common.util.UserHolder;
import com.zh.jwt.mapper.UserMapper;
import com.zh.jwt.model.dto.UserDTO;
import com.zh.jwt.model.entity.TbUser;
import com.zh.jwt.model.vo.LoginResultVO;
import com.zh.jwt.model.vo.UserVO;
import com.zh.jwt.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;

/**
 * 用户表Service实现
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, TbUser> implements UserService {
    @Override
    public ApiResult<UserVO> getUserInfo() {
        Long userId = UserHolder.getUserId();
        //使用用户Id，查询用户信息
        TbUser tbUser = baseMapper.selectById(userId);
        if (tbUser == null) {
            return ApiResult.fail("无数据");
        }
        //属性拷贝
        UserVO vo = new UserVO();
        BeanUtils.copyProperties(tbUser, vo);
        return ApiResult.success(vo);
    }

    @Override
    public ApiResult<LoginResultVO> login(UserDTO dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return ApiResult.fail("登录失败，账号或密码不能为空");
        }
        LambdaQueryWrapper<TbUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbUser::getUsername, username);
        TbUser tbUser = baseMapper.selectOne(queryWrapper);
        //用户不存在
        if (tbUser == null) {
            return ApiResult.fail(BizEnum.USER_NOT_EXIST);
        }
        //密码MD5加密
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        //校验密码
        if (tbUser.getPassword().equals(pwd)) {
            LoginResultVO vo = new LoginResultVO();
            BeanUtils.copyProperties(tbUser, vo);
            //生成Token
            String token = JwtUtil.createToken(vo.getId());
            vo.setToken(token);
            return ApiResult.success(vo);
        }
        return ApiResult.fail("登录失败，请检查用户名和密码是否正确");
    }

    @Override
    public ApiResult<Void> register(UserDTO dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return ApiResult.fail("注册信息不完整，用户名和密码不能为空");
        }
        //校验用户是否已经注册了
        LambdaQueryWrapper<TbUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbUser::getUsername, username);
        TbUser tbUser = baseMapper.selectOne(queryWrapper);
        if (tbUser != null) {
            throw new BizException(BizEnum.REGISTER_USER_IS_EXIST);
        }
        //密码MD5加密
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        //增加一个用户
        TbUser user = new TbUser();
        user.setUsername(username);
        user.setPassword(pwd);
        //创建时间
        user.setCreateTime(new Date());
        //更新时间
        user.setUpdateTime(new Date());
        baseMapper.insert(user);
        return ApiResult.success("注册成功");
    }
}